package com.scienview.core.web;


import java.util.List;
import java.util.Locale;
import java.util.Properties;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.ui.context.ThemeSource;
import org.springframework.ui.context.support.ResourceBundleThemeSource;
import org.springframework.validation.Validator;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ThemeResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
import org.springframework.web.servlet.theme.SessionThemeResolver;
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import com.scienview.core.interceptor.AuthorizationInterceptor;
import com.scienview.core.interceptor.ContextInterceptor;
 
@Configuration
@ComponentScan(basePackages="com.scienview")
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter  {

	
	
	
	/**
	 * 	<!-- ajax需要配置 -->
 	  	<mvc:annotation-driven />
 	  	
 	  	没有处理
	 */
	
	
	

	
	/**
	 * 加载属性文件
	 * @return
	 */
	@Bean
	 public static PropertyPlaceholderConfigurer properties(){
	      PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	      final Resource[] resources = new ClassPathResource[ ] {
	         new ClassPathResource( "db.properties" ),
	      };
	      ppc.setLocations( resources );
	      ppc.setIgnoreUnresolvablePlaceholders( true );
	      return ppc;
	   }
	
	/**
	 * <!-- 启动Spring MVC的注解功能，完成请求和注解POJO的映射 -->  
     	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 
     	
     	这一部分可以不写都可以实现 不知道是那一块的缘故
	 * @return
	 */
	@Bean
	public AnnotationMethodHandlerAdapter annotationMethodHandlerAdapter(){
		AnnotationMethodHandlerAdapter annotationMethodHandlerAdapter=new AnnotationMethodHandlerAdapter();
		return annotationMethodHandlerAdapter;
	}
	
	/**对模型视图名称的解析，在请求时模型视图名称添加前后缀
	 * 	<!-- 对模型视图名称的解析，在请求时模型视图名称添加前后缀 -->  
       	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
          p:prefix="/WEB-INF/pages/" p:suffix=".jsp" />  
        
       	<bean id="multipartResolver"  
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver"  
          p:defaultEncoding="utf-8" /> 
	 * */
	@Bean  
    public ViewResolver viewResolver() {  
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();  
        viewResolver.setPrefix("/WEB-INF/pages/");  
        viewResolver.setSuffix(".jsp");  
        return viewResolver;  
    }
	
	@Bean
	public MultipartResolver multipartResolver(){
		CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver();
		multipartResolver.setDefaultEncoding("UTF-8");
		return multipartResolver;
	}
	
	/**
	 * 注册国际化
	 * @return
	 */
	@Bean
	public LocaleResolver localeResolver(){
		SessionLocaleResolver localeResolver=new SessionLocaleResolver();
		localeResolver.setDefaultLocale(Locale.CHINESE);
		return localeResolver;
	}
	
	/**
	 * 注册信息
	 * @return
	 */
	@Bean
	public MessageSource messageSource(){
		ReloadableResourceBundleMessageSource messageSource=new ReloadableResourceBundleMessageSource();
		messageSource.setDefaultEncoding("UTF-8");
		messageSource.setBasename("WEB-INF/classes/conf/i18n/messages");
		return messageSource;
	}

	/**
	* 	<!-- *******************************spring主题资源配置***************************************** -->
			<bean id="themeResolver" class="org.springframework.web.servlet.theme.SessionThemeResolver">   
		    	<property name="defaultThemeName" value="default_zh_CN"/>   
		    </bean>
		    
		     <bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource">
		        <property name="basenamePrefix" value="conf/theme/"></property>
		     </bean>
	 * @return
	 */
	@Bean
	public ThemeResolver themeResolver(){
		SessionThemeResolver themeResolver=new SessionThemeResolver();
		themeResolver.setDefaultThemeName("default_zh_CN");
		return themeResolver;
	}
	
	@Bean
	public ThemeSource themeSource(){
		ResourceBundleThemeSource themeSource=new ResourceBundleThemeSource();
		themeSource.setBasenamePrefix("conf/theme/");
		return themeSource;
	}
	
	
	@Override
	public void addArgumentResolvers(
			List<HandlerMethodArgumentResolver> argumentResolvers) {
		super.addArgumentResolvers(argumentResolvers);
	}

	@Override
	public void addFormatters(FormatterRegistry registry) {
		super.addFormatters(registry);
	}

	/**
	 * 注册拦截器
	 * 	<!-- 配置拦截器  -->
 		<mvc:interceptors>
 			<!-- 控制权限 -->
 			<bean
			class="com.scienview.core.interceptor.ContextInterceptor">
		</bean>
		<bean
			class="com.scienview.core.interceptor.AuthorizationInterceptor">
		</bean>
		<!-- 国际化拦截器 -->
		<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
			<property name="paramName" value="lang" />
		</bean>
		<!-- 主题拦截器 -->
		<bean id="themeChangeInterceptor" class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
			<property name="paramName" value="theme"></property>
		</bean>
 		</mvc:interceptors>
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		/** 自定义的拦截器*/
		ContextInterceptor contextInterceptor=new ContextInterceptor();
		registry.addInterceptor(contextInterceptor);
		
		AuthorizationInterceptor authorizationInterceptor=new AuthorizationInterceptor();
		registry.addInterceptor(authorizationInterceptor);
		/** 国际化拦截器 */
		LocaleChangeInterceptor localeChangeInterceptor=new LocaleChangeInterceptor();
		localeChangeInterceptor.setParamName("lang");
		registry.addInterceptor(localeChangeInterceptor);
		/**主题拦截器*/
		ThemeChangeInterceptor themeChangeInterceptor=new ThemeChangeInterceptor();
		themeChangeInterceptor.setParamName("theme");
		registry.addInterceptor(themeChangeInterceptor);
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// TODO Auto-generated method stub
		super.addResourceHandlers(registry);
	}

	@Override
	public void addReturnValueHandlers(
			List<HandlerMethodReturnValueHandler> returnValueHandlers) {
		// TODO Auto-generated method stub
		super.addReturnValueHandlers(returnValueHandlers);
	}

	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		
		super.addViewControllers(registry);
	}

	@Override
	public void configureDefaultServletHandling(
			DefaultServletHandlerConfigurer configurer) {
		super.configureDefaultServletHandling(configurer);
	}

	/**
	 * spring mvc 统一异常处理 该处理方式
	 * <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
		    <property name="defaultErrorView">    
		        <value>errors/error</value>  
		    </property>  
		    <property name="defaultStatusCode">    
		        <value>500</value>  
		    </property> 
		    <property name="exceptionMappings">
			<props>
				<prop key="java.lang.Exception">common/error</prop>    
			</props>
			</property>    
			<property name="warnLogCategory">    
		   		 <value>org.springframework.web.servlet.handler.SimpleMappingExceptionResolver</value>  
		    </property>     
		</bean>  
	 */
	@Override
	public void configureHandlerExceptionResolvers(
			List<HandlerExceptionResolver> exceptionResolvers) {
		SimpleMappingExceptionResolver exceptionResolver=new SimpleMappingExceptionResolver();
		exceptionResolver.setDefaultErrorView("errors/error");
		exceptionResolver.setDefaultStatusCode(500);
		Properties mappings=new Properties();
		mappings.put("java.lang.Exception", "common/error");
		mappings.put("warnLogCategory", "org.springframework.web.servlet.handler.SimpleMappingExceptionResolver");
		exceptionResolver.setExceptionMappings(mappings);
		exceptionResolvers.add(exceptionResolver);
		super.configureHandlerExceptionResolvers(exceptionResolvers);
	}

	@Override
	public void configureMessageConverters(
			List<HttpMessageConverter<?>> converters) {
		// TODO Auto-generated method stub
		super.configureMessageConverters(converters);
	}

	@Override
	public Validator getValidator() {
		// TODO Auto-generated method stub
		return super.getValidator();
	}  
	
	
	
}
